<?php

namespace app\admin\controller\chient;

use app\common\controller\Backend;
use think\Db;
use function fast\e;

/**
 * 客户信息管理
 *
 * @icon fa fa-circle-o 123 gg123
 */
class Datas extends Backend
{

    /**
     * Index模型对象
     * @var \app\admin\model\chient\Index
     */
    protected $model = null;


        //数据统计
    public function index(){

        $all=Db::name('aa_client')->count();
        $this->view->assign("all", $all);


        $data1=Db::name('aa_client')->where(['types'=>1])->count();
        $this->view->assign("data1", $data1);

        $data2=Db::name('aa_client')->where(['types'=>0])->count();
        $this->view->assign("data2", $data2);

        $data2=Db::name('aa_client')->where(['ctypes'=>1])->count();
        $this->view->assign("data3", $data2);

        $data2=Db::name('aa_client')->where(['ctypes'=>2])->count();
        $this->view->assign("data4", $data2);

        $data2=Db::name('aa_client')->where(['ctypes'=>3])->count();
        $this->view->assign("data5", $data2);

        return $this->view->fetch();

    }


    //拜访信息
    public function clientlog(){


        $list =Db::name('aa_quyu')->select();

        foreach ($list as &$key){

            $u=Db::name('user')->where(['id'=>$key['user_id']])->find();
            if ($u){
                $key['text']=$key['text'].'_'.$u['nickname'];
            }

        }

        $createtime=date('Y-m');
        $endtime=date('Y-m-d');


        $this->assign('time',['a'=>$createtime,'b'=>$endtime]);

        $this->assign('list',$list);
        return $this->view->fetch();


    }

    // 拜访信息-> 数据 123
    public function clientlogajax(){
        $filter=$this->request->param('filter');


        $filter=json_decode($filter,true);

        $map['a.id']=['gt',0];

        if (isset($filter['quyuid'])){
            $quyuid=$filter['quyuid'];
            $map['a.quyu_id']=['eq',$quyuid];

        }
        if (isset($filter['merchname'])){
            $merchname=$filter['merchname'];
            $map['b.name']=['like','%'.$merchname.'%'];

        }

        if (isset($filter['types'])){
            $types=$filter['types'];
            $map['a.types']=['eq',$types];

        }

        if (isset($filter['title'])){
            $merchname=$filter['title'];
            $map['a.title']=['like','%'.$merchname.'%'];

        }


        if (isset($filter['createtime'])){
            $createtime=$filter['createtime'];
            $times=explode(' - ',$createtime);
            $strtime=strtotime($times[0]);
            $endtime=strtotime($times[1]);

            $map['a.time']=['between' , "$strtime,$endtime"];
        }else{

           $createtime=strtotime(date('Y-m'));
           $endtime=strtotime(date('Y-m-d'));
           $map['a.time']=['between' , "$createtime,$endtime"];

        }



        $offset=$this->request->param('offset');
        $limit=$this->request->param('limit');


        $list=Db::name('client_log')->alias('a')
            ->join('aa_client b','a.aa_client_id=b.id')
            ->where($map)
            ->field('a.*,b.name,b.address')
            ->limit($offset,$limit)
            ->order('a.id desc')->select();


        $count=Db::name('client_log')->alias('a')
            ->join('aa_client b','a.aa_client_id=b.id')
            ->where($map)
            ->count();

//        echo  Db::getLastSql();

        foreach ($list  as &$key){


            $key['time']=date('Y-m-d H:i',$key['time']);

            $user=Db::name('user')->where(['id'=>$key['user_id']])->find();

            if ($user){
                $key['nickname']=$user['nickname'];
            }

            $key['typesv']=$key['types']==1?'目标客户':'非目标客户';

        }

        $result = array("total" => $count, "rows" => $list);
        return json($result);

    }


    //未走访
    public function nosee(){
        $list =Db::name('aa_quyu')->select();
        $this->assign('list',$list);
        return $this->view->fetch();
    }

    //未走访 ->数据

    public function noseeajax(){

        $filter=$this->request->param('filter');

        $filter=json_decode($filter,true);

        $map['a.id']=['gt',0];
        if (isset($filter['quyuid'])){
            $quyuid=$filter['quyuid'];
            $map['a.quyu_id']=['eq',$quyuid];
        }

        $offset=$this->request->param('offset');
        $limit=$this->request->param('limit');



        $list=Db::name('aa_client')->alias('a')
            ->join('aa_quyu b','a.quyu_id=b.id')
            ->where($map)
            ->limit($offset,$limit)
            ->field('a.*,b.text')
            ->select();

        $count=Db::name('aa_client')->alias('a')
            ->join('aa_quyu b','a.quyu_id=b.id')
            ->where($map)
            ->count();


        if (isset($filter['createtime'])){
            $createtime=$filter['createtime'];
            $times=explode(' - ',$createtime);
            $strtime=strtotime($times[0]);
            $endtime=strtotime($times[1]);
            $mapb['time']=['between' , "$strtime,$endtime"];
        }




        $array=[];

        foreach ($list  as $key=>$v){

            $mapb['aa_client_id']=['eq',$v['id']];
            $see=Db::name('client_log')
                ->where($mapb)
                ->find();

            $v['time']=date('Y-m-d H:m',$v['time']);

            if (!$see){
                $array[]=$v;
            }

        }

        $result = array("total" => $count, "rows" => $array);
        return json($result);


    }


    //工作时间
    public function hour(){

        $list =Db::name('aa_quyu')->select();

        foreach ($list as &$key){
            $u=Db::name('user')->where(['id'=>$key['user_id']])->find();

            if ($u){
                $key['text']=$key['text'].'_'.$u['nickname'];
            }

        }


        $this->assign('list',$list);
        return $this->view->fetch();

    }

    //未走访 ->数据
    public function hourajax(){

        $filter=$this->request->param('filter');

        $filter=json_decode($filter,true);


        $map1['a.id']=['gt',0];
        if (isset($filter['quyuid'])){
            $map1['a.id']=['eq',$filter['quyuid']];
        }


        $list=Db::name('aa_quyu')->alias('a')
            ->join('user b','a.user_id=b.id')
            ->where($map1)
            ->field('a.*,b.nickname,b.mobile')
            ->select();

        $mapb=[];
        if (isset($filter['createtime'])){
            $createtime=$filter['createtime'];
            $times=explode(' - ',$createtime);
            $strtime=strtotime($times[0]);
            $endtime=strtotime($times[1]);
            $mapb['startime']=['between' , "$strtime,$endtime"];
        }

        foreach ($list  as &$key){

            $mapb['quyu_id']=['eq' ,$key['id']];

            $hour=Db::name('workhour')->where($mapb)->sum('hour');

            $key['hour']=$hour;
        }

        $order=$this->request->param('order');

        if ($order=='desc'){
            $list=  bubbleSorts($list);
        }else{
            $list=  bubbleSortsb($list);

        }

        $result = array("total" => count($list), "rows" => $list);

        return json($result);


    }


    //客户信息
    public function users(){

        $list =Db::name('aa_quyu')->select();

        foreach ($list as &$key){
            $u=Db::name('user')->where(['id'=>$key['user_id']])->find();

            if ($u){
                $key['text']=$key['text'].'_'.$u['nickname'];
            }

        }


        $this->assign('list',$list);
        return $this->view->fetch();

    }


    // 拜访信息-> 数据
    public function usersajax(){
        $filter=$this->request->param('filter');


        $filter=json_decode($filter,true);

        $map['a.id']=['gt',0];

        if (isset($filter['quyuid'])){
            $quyuid=$filter['quyuid'];
            $map['a.quyu_id']=['eq',$quyuid];
        }


        if (isset($filter['types'])){
            $types=$filter['types'];
            $map['a.types']=['eq',$types];
        }
        if (isset($filter['ctypes'])){
            $dtypes=$filter['ctypes'];
            $map['a.ctypes']=['eq',$dtypes];
        }



        if (isset($filter['createtime'])){
            $createtime=$filter['createtime'];
            $times=explode(' - ',$createtime);
            $strtime=strtotime($times[0]);
            $endtime=strtotime($times[1]);

            $map['a.time']=['between' , "$strtime,$endtime"];
        }


        $list=Db::name('aa_client')->alias('a')
            ->join('aa_quyu b','a.quyu_id=b.id')
            ->where($map)
            ->field('a.*,b.text,b.user_id')
            ->select();

        foreach ($list  as &$key){
            $key['time']=date('Y-m-d H:i',$key['time']);

            $key['types']=$key['types']==1?'目标客户':'非目标客户';


            $user=Db::name('user')->where(['id'=>$key['user_id']])->find();
            if ($user){
                $key['nickname']=$user['nickname'];
            }

        }

        $result = array("total" => count($list), "rows" => $list);
        return json($result);

    }


    //分类客户

    public function userb(){

        $list =Db::name('aa_quyu')->select();

        foreach ($list as &$key){
            $u=Db::name('user')->where(['id'=>$key['user_id']])->find();

            if ($u){
                $key['text']=$key['text'].'_'.$u['nickname'];
            }



        }


        $this->assign('list',$list);
        return $this->view->fetch();

    }


    // 拜访信息-> 数据
    public function userbajax(){
        $filter=$this->request->param('filter');


        $filter=json_decode($filter,true);

        $map['a.id']=['gt',0];

        if (isset($filter['quyuid'])){
            $quyuid=$filter['quyuid'];
            $map['a.quyu_id']=['eq',$quyuid];
        }


        if (isset($filter['types'])){
            $quyuid=$filter['types'];
            $map['a.ctypes']=['eq',$quyuid];
        }

        if (isset($filter['createtime'])){
            $createtime=$filter['createtime'];
            $times=explode(' - ',$createtime);
            $strtime=strtotime($times[0]);
            $endtime=strtotime($times[1]);

            $map['a.ctypestime']=['between' , "$strtime,$endtime"];
        }


        $list=Db::name('aa_client')->alias('a')
            ->join('aa_quyu b','a.quyu_id=b.id')
            ->where($map)
            ->field('a.*,b.text')
            ->select();

        foreach ($list  as &$key){
            $key['time']=date('Y-m-d H:i',$key['time']);

            $key['types']=$key['types']==1?'目标客户':'非目标客户';
        }

        $result = array("total" => count($list), "rows" => $list);
        return json($result);

    }


    //月排名
    public function orders()
    {

        $arr=[];

        for ($int=0;$int<8;$int++){

            $str= strtotime('-'.$int.' month');
            $arr[]=date('Y-m',$str);

        }

        $this->assign('dates',$arr);

        return $this->view->fetch();

    }

    public function ordersajax(){

        $list=Db::name('aa_quyu')->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname')
            ->select();

        $set=Db::name('work_set')->find();

        $md=$set['number'];
        $ds=$set['number1'];


        $filter=$this->request->param('filter');


        $filter=json_decode($filter,true);

        if  (isset($filter['quyuid'])){
            $adddate=$filter['quyuid'];
        }else{
            $adddate=date('y-m');
        }


        $strtime=strtotime($adddate);

        $endtime=strtotime('+1 month',$strtime);


        $map['time']=['between' , "$strtime,$endtime"];

        $satev=date('ym',$strtime);

        $map['types']=['eq' ,1];

        foreach ($list as &$key){

            //本月拜访次数

            $count=Db::name('client_log')
                ->where($map)
                ->where('quyu_id','eq',$key['id'])
                ->count();
            $key['count']=$count;

            //本月请假
            $holiday=Db::name('holiday')
                ->where('date','eq',$satev)
                ->where(['user_id'=>$key['user_id']])->count();

            $bili=0;
            if ($count>0){
              $bili=round( $count/($ds*($md-$holiday)),2);
            }

            $key['bili']=$bili;

        }


        $order=$this->request->param('order');

        if ($order=='desc'){
            $list=bubbleSortb($list);
        }else{
            $list=bubbleSortg($list);
        }



        foreach ($list as &$key){
            $key['bili']=$key['bili'].'%';
        }




        $result = array("total" => count($list), "rows" => $list);

        return json($result);


    }





}
